export default {
	data() {
		return {
			user_preporty: {},
		}
	},
	methods: {
		// 获取微信用户的信息
		handleWechat(callBack){			
			uni.getUserProfile({
				desc: '用于完善会员资料',
				success: infoRes => {
					uni.login({
						provider: 'weixin',
						success: loginRes => {
							let params = {
								code: loginRes.code,
								user_info: infoRes.userInfo,
							}
							callBack(params);
						}
					})
				}
			})
		},

		// 绑定微信
		bindWechat(userName) {
			this.handleWechat(params => {
				let obj = {
					userName: userName,
					wechatCode: params.code,
					encryptedData: params.encryptedData,
					iv: params.iv
				};
				this.$http('put', '/nursing-upms/user/bindWechat', Object({}, this.wx_data, obj), (data, msg) => {
					uni.showToast({
						title: '绑定微信成功！',
						icon: 'none'
					});
					this.loginByWechat(params);
				});
			});
		},
		// 获取用户信息
		getUserInfo(callback) {
			this.$http('get', '/admin/user/info', {}, (data, msg) => {
				this.user_info = data.sysUser;
				this.user_property = data.property;
				uni.setStorageSync('userInfo', data.sysUser);
				this.$store.commit('setUserInfo', data.sysUser);
				if(typeof(callback) === 'function'){
					callback();
			}
				if (data.status == 0) {
					uni.showModal({
						title: '提示',
						content: '您的账号已被冻结，有疑问请联系客服',
					})
				}
			})
		},
		// 返回登录的上一页
		backFromLoginPage() {
			let before_login = uni.getStorageSync('before_login');
			let switchTab = [
				'/pages/index/index',
				'/pages/order/index',
				'/pages/community/index',
				'/pages/usercenter/index',
			];			
			console.log('befor_login', before_login);
			if (before_login) {
				if(switchTab.includes(before_login)){
					uni.switchTab({
						url: before_login
					})
				}else{
					uni.reLaunch({
						url: before_login
					});
				}
				uni.removeStorageSync('before_login');
			} else {
				uni.switchTab({
					url: '/pages/index/index'
				})
			}
		},
		// 注册时，检测用户名是否可用
		async checkUserName(userName) {
			let can_use = await this.$http('get', '/nursing-admin/scs/account/userNameExist', {
				userName: userName
			});
			if (can_use) {
				return userName;
			} else {
				let user_name = 'yjh' + this.$utils.randomStr(9);
				this.checkUserName(user_name);
			}
		},
		// 注册时，检测手机号是否可用
		async checkPhone(phone) {
			let can_use = await this.$http('get', '/nursing-admin/scs/account/phoneExist', {
				phone: phone
			});
			return can_use;
		},
	}
}